<?php

namespace app\admin\controller;

use app\common\lib\CusSession;
use think\Controller;

class Index extends Father
{
    //登陆验证初始化方法



    public function getUser(){

        $user = CusSession::get($this->_login_key);
        $json = [
            'data'=>$user,
            'success' =>true,
            'messages' =>'登录信息',
        ];
        header('Content-Type: application/json; charset=UTF-8');
        exit(json_encode($json));
    }

    //登陆模块
    public function Login(){

        $json = [
            'data'=>'',
            'success' =>false,
            'messages' =>'登陆失败',
        ];
            $data = [
                'username' => input('post.username'),
                'password' => input('post.password')
            ];
            $result =model('Login')->login($data);

            if ($result == 1) {
                $sessionData = model('Admin')->where('username', $data['username'])->find();
                CusSession::set($this->_login_key, $sessionData);
                $json = [
                    'token_id'=>CusSession::$sid,
                    'success' =>true,
                    'messages' =>'登陆成功',
                ];

               return json($json);
            } else {
                $json['success'] = false;
                $json['messages'] = $result;

                return  json($json);
            }


        return   json($json);
    }
    //图片上传
    public function upload_picture(){
        $file = request()->file('picture');
        $info = $file->move( './upload');
        $address = '/upload/'.$info->getSaveName();
        $json = [
            'date'=>$address,
            'success' =>true,
            'messages' =>'上传成功',
        ];

        return json($json);
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
            echo $info->getExtension()."<br/>";
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            echo $info->getSaveName()."<br/>";
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            echo $info->getFilename()."<br/>";
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }

    }

}
